import React from 'react';
import style from './ImageSlider.css';
import AspectRatioImage from "./AspectRatioImage";
import PropTypes from 'prop-types';

function Image({src, show}) {
  const className = show ? `${style.image} ${style.imageActive}` : style.image;
  return (
    <div className={className}>
      <AspectRatioImage
        image={src}
        ratio={0.625}
      />
    </div>
  );
}

class ControllerItem extends React.Component {

  onClick = (event) => {
    if(!this.props.onClick) {
      return;
    }
    this.props.onClick(event, this.props.index);
  };

  render() {
    let className = style.controllerItem;
    if(this.props.active) {
      className = `${style.controllerItem} ${style.controllerItemActive}`;
    }
    return (
      <div
        role="button"
        className={className}
        onClick={this.onClick}
        style={{ transitionDuration: this.props.transitionDuration }}
      />
    );
  }
}

ControllerItem.propTypes = {
  active: PropTypes.bool,
  onClick: PropTypes.func,
  transitionDuration: PropTypes.number
};

ControllerItem.defaultProps = {
  active: false,
  onClick: null,
  transitionDuration: 200
};

class ImageSlider extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      current: 0
    };
  }

  componentDidMount() {
    this.startInterval();
  }

  componentWillUnmount() {
    this.stopInterVal();
  }

  onControllerItemClick = (e, index) => {
    this.stopInterVal();
    this.setState({
      current: index % this.props.image.length
    });
    this.startInterval();
  };

  startInterval = () => {
    this.stopInterVal();
    this.sliderInterval = setInterval(() => {
      this.setState({
        current: (this.state.current + 1) % this.props.images.length
      });
    }, this.props.period);
  };

  stopInterVal = () => {
    if(this.sliderInterval) {
      clearInterval(this.sliderInterval);
    }
  };

  mapImage = (image, index) => (
    <Image
      key={`${this.props.key}.image${index}`}
      src={image}
      show={index === this.state.current}
    />
  );

  renderControllerItem = () => {
    const items = [];
    for(let i = 0; i < this.props.images.length; i++) {
      items.push(
        <ControllerItem
          key={`${this.props.key}.controller${i}`}
          index={i}
          active={i === this.state.current}
          transitionDuration={this.props.transitionDuration}
          onClick={this.onControllerItemClick}
        />
      );
    }
    return items;
  };

  render() {
    return (
      <div className={style.container} style={this.props.style}>
        <div className={style.imageWrapper} style={{ paddingTop: '62.5%' }}>
          {this.props.images.map(this.mapImage)}
        </div>
        <div className={`${style.controller} clearfix`}>
          {this.renderControllerItem()}
        </div>
      </div>
    );
  }
}

ImageSlider.propTypes = {
  images: PropTypes.arrayOf(PropTypes.string).isRequired,
  period: PropTypes.number,
  transitionDuration: PropTypes.number
};

ImageSlider.defaultProps = {
  period: 5000,
  transitionDuration: 200
};

export default ImageSlider;